package com.luxk.db.server;

import java.io.File;

public class StartupTest {

	private StartupMode mode = StartupMode.NONE;
	private String xmlFilePath = "";
	private String xmlFileName = "server.xml";
	
	private Server server = null;

	public void exec() throws Exception {

		String serverHome = System.getProperty("server.home");

		if(serverHome == null || "".equals(serverHome)) {
			throw new ServerBuildException("Property server.home not found");
		}

		System.setProperty("user.dir", serverHome);

		xmlFilePath = serverHome + File.separator +
				"conf" + File.separator +
				xmlFileName;

		ServerInstanceBuilder builder = new ServerInstanceBuilder();
		this.server = builder.build(xmlFilePath);

		this.server.init();
		this.server.startup(this.mode);

		this.server.runTest();

		this.server.shutdown(ShutdownMode.NORMAL);
	}
	
	public void useage() {
		System.out.println("Useage : StartupTest [mode]");
		System.out.println("  mode NOMOUNT|MOUNT|NORMAL : Server test mode");
	}

	public void processArgs(String[] args) {
		
		if(args.length > 0) {
			for(StartupMode m: StartupMode.values())
				if(m.getStrVal().equalsIgnoreCase(args[0])) {
					this.mode = m;
					return;
				}
		} else {
			// 테스트는 기본적으로 단위테스트이므로 db가 생성되지 않은 상태로 테스트가 default.
			// 따라서 아무 옵션을 주지 않으면 nomount모드로 테스트한다.
			this.mode = StartupMode.NOMOUNT;
		}
	}

	public static void main(String[] args) {
		
		try {
			StartupTest startup = new StartupTest();
			startup.processArgs(args);
			if(startup.mode != StartupMode.NONE) {
				startup.exec();
			} else {
				startup.useage();
			}

			System.exit(0);

		} catch(Exception e) {
			e.printStackTrace();
		}
	}

}
